<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

/**
 * AdhesionController
 * 
 * @package JL Panel
 * @author JLQ
 * @copyright 2009
 * @version 1.0
 * @access public
 */
class AdhesionController extends JLControllerBase {

    private $config = null;
    private $path = "Adhesion";
    private $title = "Système d'Actualisation CSME > Adhesion";
    private $data = null;
    private $dirAdhesion = "adhesion";

    /**
     * AdhesionController::updateMembreOrder()
     * 
     * @return
     */
    function getAdhesion() {
        $id = $_POST['_id'];

        require JLPATH_MODELS . DS . "AdhesionModel.php";
        $Adhesions = new AdhesionModel();
        $Adhesion = $Adhesions->getAdhesionById($id);
        $var_title_link = $Adhesion['var_title_link'];
        $var_link_pdfword = $Adhesion['var_link_pdfword'];
        $int_id_state = $Adhesion['int_id_state'];

        echo $var_title_link . "_%_" . $var_link_pdfword . "_%_" . $int_id_state;
    }

    /**
     * AdhesionController::updateAdhesion()
     * 
     * @return
     */
    function updateAdhesion() {
        $id = $_POST['_id'];
        $var_title_link = $_POST['_var_title_link'];
        $var_link_pdfword = $_POST['_var_link_pdfword'];
        $int_id_sections_view = $_POST['_int_id_sections_view'];
        $int_id_state = $_POST['_int_id_state'];

        require JLPATH_MODELS . DS . 'SectionModel.php';
        $sections = new SectionModel();
        $section = $sections->getSectionByID(intval($int_id_sections_view) + 2);
        $seccion = $section['var_desc_section'];

        $jl = new JLTools();
        $seccion = $jl->HTMLTranslate($seccion, false);
        $seccion = $jl->replaceCaracteresAcentuados($seccion);
        $seccion = strtolower($jl->removeSpecialCaracter($seccion));

        $newspath = JLPATH_WEB_SITE . DS . $this->dirAdhesion . DS . $seccion;

        $var_link_pdfword = JLPATH_WEB_SITE . DS . $this->dirAdhesion . DS . basename($var_link_pdfword);
        $var_link_pdfword2 = $newspath . DS . basename($var_link_pdfword);

        require JLPATH_MODELS . DS . "AdhesionModel.php";
        $Adhesions = new AdhesionModel();
        $linkn = $Adhesions->getAdhesionById($id);
        $dirr = JLPATH_WEB_SITE . DS . $this->dirAdhesion . DS . $seccion . DS . basename($linkn['var_link_pdfword']);

        $bool = @copy($var_link_pdfword, $var_link_pdfword2);
        if ($bool) {
            @unlink($dirr);
            @unlink($var_link_pdfword);
        }

        /* if( strtolower($_SERVER['HTTP_HOST']) != strtolower(basename( JLPATH_WEB_SITE )))
          $filePDF =  $jl->getNameDomain()."/".basename( JLPATH_WEB_SITE )."/".$this->dirAdhesion."/".$seccion."/".basename( $var_link_pdfword );
          else */
        $filePDF = $jl->getNameDomain() . "/" . $this->dirAdhesion . "/" . $seccion . "/" . basename($var_link_pdfword);

        $_true = $Adhesions->updateAdhesion($id, $var_title_link, $filePDF, $int_id_sections_view, $int_id_state);

        if ($_true)
            echo "Données sauvegarder avec succès.";
        else
            echo "";
    }

    /**
     * AdhesionController::insertAdhesion()
     * 
     * @return
     */
    function insertAdhesion() {
        $var_title_link = $_POST['_var_title_link'];
        $var_link_pdfword = $_POST['_var_link_pdfword'];
        $int_id_sections_view = $_POST['_int_id_sections_view'];
        $int_id_state = $_POST['_int_id_state'];

        require JLPATH_MODELS . DS . 'SectionModel.php';
        $sections = new SectionModel();
        $section = $sections->getSectionByID(intval($int_id_sections_view) + 2);
        $seccion = $section['var_desc_section'];

        $jl = new JLTools();
        $seccion = $jl->HTMLTranslate($seccion, false);
        $seccion = $jl->replaceCaracteresAcentuados($seccion);
        $seccion = strtolower($jl->removeSpecialCaracter($seccion));

        $newspath = JLPATH_WEB_SITE . DS . $this->dirAdhesion . DS . $seccion;
        $jl->MakeDir($newspath);

        $var_link_pdfword = JLPATH_WEB_SITE . DS . $this->dirAdhesion . DS . basename($var_link_pdfword);
        $var_link_pdfword2 = $newspath . DS . basename($var_link_pdfword);

        $bool = @copy($var_link_pdfword, $var_link_pdfword2);
        if ($bool)
            @unlink($var_link_pdfword);

        /* if( strtolower($_SERVER['HTTP_HOST']) != strtolower(basename( JLPATH_WEB_SITE )))
          $filePDF =  $jl->getNameDomain()."/".basename( JLPATH_WEB_SITE )."/".$this->dirAdhesion."/".$seccion."/".basename( $var_link_pdfword );
          else */
        $filePDF = $jl->getNameDomain() . "/" . $this->dirAdhesion . "/" . $seccion . "/" . basename($var_link_pdfword);

        require JLPATH_MODELS . DS . "AdhesionModel.php";
        $adhesions = new AdhesionModel();
        $_true = $adhesions->insertAdhesion($var_title_link, $filePDF, $int_id_sections_view, $int_id_state);

        if ($_true)
            echo "Données sauvegarder avec succès.";
        else
            echo "";
    }

    function getIconFile($pathFile) {
        $jl = new JLTools();
        switch ($jl->getFileExtension($pathFile)) {
            case "jpg":
                $img = '<img src="' . $jl->getNameDomain() . '/images' . DS . '24x24' . DS . 'picture.png">';
                break;
            case "doc";
            case "docx";
                $img = '<img src="' . $jl->getNameDomain() . '/images' . DS . '24x24' . DS . 'word.png">';
                break;
            case "xlsx";
            case "xls";
                $img = '<img src="' . $jl->getNameDomain() . '/images' . DS . '24x24' . DS . 'excel.png">';
                break;
        }

        return str_replace(trim('\ '), "/", $img);
    }

    function getFilePath() {
        $path = $_POST['_path'];
        require JLPATH_MODELS . DS . 'SectionModel.php';
        $sections = new SectionModel();
        $section = $sections->getSectionByID(intval($_POST['_id']) + 2);
        $seccion = $section['var_desc_section'];

        $jl = new JLTools();
        $seccion = $jl->replaceAccentCharacters($seccion);

        if (strtolower($_SERVER['HTTP_HOST']) != strtolower(basename(JLPATH_WEB_SITE)))
            $newspath = $jl->getNameDomain() . "/" . $this->dirAdhesion . "/" . $seccion . "/" . basename($path);
        else
            $newspath = $jl->getNameDomain() . "/" . $this->dirAdhesion . "/" . $seccion . "/" . basename($path);

        echo $newspath;
    }

    /**
     * SectionsController::deleteMembre()
     * 
     * @return
     */
    function deleteAdhesion() {
        $ids = $_POST['_ids'];
        $ides = explode(",", $ids);
        require JLPATH_MODELS . DS . 'SectionModel.php';
        $sections = new SectionModel();
        $jl = new JLTools();

        require JLPATH_MODELS . DS . "AdhesionModel.php";
        $adhesions = new AdhesionModel();

        $i = 0;
        foreach ($ides as $id) {
            $adhesion = $adhesions->getAdhesionById($id);
            $adhesionpath = $adhesion['var_link_pdfword'];
            $int_id_sections_view = $adhesion['int_id_sections_view'];

            $_true = $adhesions->DeleteAdhesion($id);
            $section = $sections->getSectionByID(intval($int_id_sections_view) + 2);
            $seccion = $section['var_desc_section'];

            $seccion = $jl->HTMLTranslate($seccion, false);
            $seccion = $jl->replaceCaracteresAcentuados($seccion);
            $seccion = strtolower($jl->removeSpecialCaracter($seccion));

            $newspath = JLPATH_WEB_SITE . DS . $this->dirAdhesion . DS . $seccion . DS . basename($adhesionpath);

            if ($_true) {
                if (is_file($newspath))
                    @unlink($newspath);
            }else
                break;

            $i++;
        }

        $nodelete = count($ides) - $i;

        if (!$_true) {
            echo $i . "  Donnée(s) effacer \n$nodelete donnée(s) n'a pu être effacer. Veuillez changer l'état à Provisoire.";
        }
        else
            echo $i . "  Donnée(s) effacer";
    }

    function CutImage() {
        $targ_w = $this->_x;
        $targ_h = $this->_y;
        $jpeg_quality = $this->_quality;

        $src = JLPATH_WEB_SITE . DS . "images" . DS . "membres" . DS . $_POST['_path'];

        $img_r = @imagecreatefromjpeg($src);
        $dst_r = @ImageCreateTrueColor($targ_w, $targ_h);

        @imagecopyresampled($dst_r, $img_r, 0, 0, $_POST['x'], $_POST['y'], $targ_w, $targ_h, $_POST['w'], $_POST['h']);

        $b = @imagejpeg($dst_r, $src, $jpeg_quality);
        if ($b)
            echo "OK";
    }

}